package com.ourteam.system.dao.impl;

import com.ourteam.system.dao.ISysMessageKeyDAO;
import com.ourteam.system.dao.SysMessageKey;
import com.ourteam.system.dao.SysMessageKeyDAOQueryBean;

import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Template Name : DAO服务对象
 * Template Version : 1
 * Template Last Modfiy Date : Fri Nov 17 11:00:11 CST 2017
 * Auto Generate Date : 2018-07-31 15:13:35
  * Warning ! This file will be overwrite when regenerate !
  *
 */
public class AbstractSysMessageKeyDAOJdbcImpl extends AbstractSpringDAO
    implements ISysMessageKeyDAO {
    public static final String[] ENTITY_FIELDS = new String[] {
            Id, MessageCatalog, MessageKey, Remarks, Status
        };
    public static final String[] ENTITY_PROPERTIES = new String[] {
            _DataObjectProperty.Id, _DataObjectProperty.MessageCatalog,
            _DataObjectProperty.MessageKey, _DataObjectProperty.Remarks,
            _DataObjectProperty.Status
        };
    public static final String[] ENTITY_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {  };
    private static String relatedJoinSQL = null;
    private static String basicFilterSQL = null;
    private DataAccessObjectHelper<SysMessageKey, SysMessageKeyDAOQueryBean> daoHelper;
    private com.ourteam.system.dao.impl.AbstractSysMessageResourceDAOJdbcImpl SysMessageResourceDao;

    public AbstractSysMessageKeyDAOJdbcImpl() {
        super();
        daoHelper = new DataAccessObjectHelper<SysMessageKey, SysMessageKeyDAOQueryBean>(this);
    }

    protected String getEntityName() {
        return EntityName;
    }

    protected String[] getEntityFields() {
        return ENTITY_FIELDS;
    }

    protected String[] getEntityProperties() {
        return ENTITY_PROPERTIES;
    }

    protected String[] getEntityPKFields() {
        return ENTITY_PK_FIELDS;
    }

    protected String[] getEntityAutoGenPKFields() {
        return ENTITY_AUTO_GEN_PK_FIELDS;
    }

    protected String[] getEntityUniqueFields() {
        return ENTITY_UNIQUE_FIELDS;
    }

    protected String getBaiscQueryFilter() {
        if (basicFilterSQL == null) {
            StringBuffer basicFilterSQLbuffer = new StringBuffer();

            basicFilterSQL = basicFilterSQLbuffer.toString();
        }

        return basicFilterSQL;
    }

    protected String getRelatedJoinSQL() {
        if (relatedJoinSQL == null) {
            StringBuffer relatedJoinSQLBuffer = new StringBuffer();

            relatedJoinSQL = relatedJoinSQLBuffer.toString();
        }

        return relatedJoinSQL;
    }

    /**
         * Check Data Unique
         * @param aSysMessageKey
         * @throws Exception
         */
    public boolean checkIsUnique(SysMessageKey aSysMessageKey)
        throws Exception {
        if (aSysMessageKey == null) {
            return true;
        }

        List valueList = new ArrayList();

        List pkValueList = new ArrayList();

        pkValueList.add(aSysMessageKey.getId());

        return isUnique(valueList.toArray(new Object[valueList.size()]),
            pkValueList.toArray(new Object[pkValueList.size()]));
    }

    /**
     * Insert Data
     * @param aSysMessageKey
     * @throws Exception
     */
    public void insert(SysMessageKey aSysMessageKey) throws Exception {
        super.doInsert(aSysMessageKey);
    }

    /**
         * Batch Insert Datas
         * @param aSysMessageKey
         * @throws Exception
         */
    public void batchInsert(SysMessageKey[] aSysMessageKeys)
        throws Exception {
        super.doBatchInsert(aSysMessageKeys);
    }

    /**
         * Delete Data
         * @param  final long id         *
         * @throws Exception
         */
    public void delete(final long id) throws Exception {
        super.doDeleteById(new Object[] { new Long(id) });
    }

    /**
     * Delete Datas
     * @param queryBean
     * @throws Exception
     */
    public void delete(SysMessageKeyDAOQueryBean queryBean)
        throws Exception {
        super.doDeleteByQuery(queryBean);
    }

    public void delete(SysMessageKey aSysMessageKey) throws Exception {
        delete(aSysMessageKey.getId());
    }

    /**
     * Update Data Selective
     * @param aSysMessageKey
     * @throws Exception
     */
    public void update(SysMessageKey aSysMessageKey) throws Exception {
        if (aSysMessageKey.getId() == 0) {
            throw new Exception(
                "Can not update object data whith out primary key value");
        }

        super.doUpdate(aSysMessageKey);

        SysMessageKey tempObj = findById(aSysMessageKey.getId());

        if (tempObj != null) {
            PropertyUtils.copyProperties(aSysMessageKey, tempObj);
        }
    }

    /**
     * Batch Update Datas
     * @param aSysMessageKeys[]
     * @throws Exception
     */
    public void batchUpdate(SysMessageKey[] aSysMessageKeys)
        throws Exception {
        super.doBatchUpdate(aSysMessageKeys);
    }

    /**
         * Update Data By Condictions
         * @param aSysMessageKey
         * @param aValueCriteria
         * @param aTransaction
         * @throws Exception
         */
    public void update(SysMessageKey aSysMessageKey,
        SysMessageKeyDAOQueryBean aQueryBean) throws Exception {
        super.doUpdateByQuery(aSysMessageKey, aQueryBean);
    }

    /**
     * Update Data By Condictions
     * @param aQueryBean
     * @throws Exception
     */
    public void update(SysMessageKeyDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aQueryBean);
    }

    protected void checkNullValue(SysMessageKey aSysMessageKey)
        throws Exception {
    }

    /**
     * Get Data By ID
     * @param  final long id         * @return  ${businessObject.name}
     * @throws Exception
     */
    public SysMessageKey findById(final long id) throws Exception {
        SysMessageKeyDAOQueryBean queryBean = new SysMessageKeyDAOQueryBean();

        SysMessageKeyDAOQueryBean.Criteria criteria = queryBean.createCriteria();

        criteria.andIdEqualTo(id);

        //queryBean.setDistinct(true);
        SysMessageKey[] SysMessageKeys = query(queryBean);

        if (SysMessageKeys.length == 0) {
            return null;
        } else {
            return SysMessageKeys[0];
        }
    }

    /**
         * Query Data
         * @param queryBean
         * @return SysMessageKey[]
         * @throws Exception
         */
    public SysMessageKey[] query(SysMessageKeyDAOQueryBean queryBean)
        throws Exception {
        List result = super.doQuery(queryBean, new JdbcSysMessageKeyRowMapper());

        return (SysMessageKey[]) result.toArray(new SysMessageKey[result.size()]);
    }

    /**
     * Query ${businessObject.name} Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySelective(
        SysMessageKeyDAOQueryBean queryBean) throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addAllSelectProperties();
        }

        return super.doQuerySelective(queryBean);
    }

    /**
     * Query Data Count
     * @param queryBean
     * @return int
     * @throws Exception
     */
    public int queryCount(SysMessageKeyDAOQueryBean queryBean)
        throws Exception {
        return super.doQueryCount(queryBean);
    }

    public void synchronize(SysMessageKey[] aSysMessageKeys,
        SysMessageKeyDAOQueryBean queryBean) throws Exception {
        daoHelper.synchronize(aSysMessageKeys, queryBean);
    }

    public void synchronize(SysMessageKey[] aSysMessageKeys,
        SysMessageKeyDAOQueryBean queryBean,
        net.dao.IDataAccessInterface.ISynchronizeCallback<SysMessageKey> callBack)
        throws Exception {
        daoHelper.synchronize(aSysMessageKeys, queryBean, callBack);
    }

    public void setSysMessageResourceDao(
        com.ourteam.system.dao.ISysMessageResourceDAO SysMessageResourceDao) {
        this.SysMessageResourceDao = (com.ourteam.system.dao.impl.AbstractSysMessageResourceDAOJdbcImpl) SysMessageResourceDao;
    }

    /**
     * Query Related SysMessageResource
    * @param queryBean
     * @throws Exception
     */
    public com.ourteam.system.dao.SysMessageResource[] querySysMessageResource(
        SysMessageKeyDAOQueryBean queryBean) throws Exception {
        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.system.dao.ISysMessageResourceDAO.MessageKeyId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.system.dao.impl.SysMessageResourceDAO.EntityName +
            " ref_SysMessageResource";

        String[] entityFields = com.ourteam.system.dao.impl.SysMessageResourceDAO.ENTITY_FIELDS;

        String[] entityProperties = com.ourteam.system.dao.impl.SysMessageResourceDAO.ENTITY_PROPERTIES;

        List list = SysMessageResourceDao.doQuery(entityName, entityFields,
                entityProperties, queryBean, joinSQL.toString(),
                new com.ourteam.system.dao.impl.SysMessageResourceDAO.JdbcSysMessageResourceRowMapper());

        return (com.ourteam.system.dao.SysMessageResource[]) list.toArray(new com.ourteam.system.dao.SysMessageResource[list.size()]);
    }

    /**
     * Query Related SysMessageResource Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySysMessageResourceSelective(
        SysMessageKeyDAOQueryBean queryBean) throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addSelectProperty(com.ourteam.system.dao.ISysMessageResourceDAO.Id,
                "id");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysMessageResourceDAO.LocalCode,
                "localCode");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysMessageResourceDAO.MessageKeyId,
                "messageKeyId");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysMessageResourceDAO.MessageValue,
                "messageValue");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysMessageResourceDAO.Remarks,
                "remarks");

            queryBean.addSelectProperty(com.ourteam.system.dao.ISysMessageResourceDAO.Status,
                "status");
        }

        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.system.dao.ISysMessageResourceDAO.MessageKeyId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.system.dao.impl.SysMessageResourceDAO.EntityName +
            " ref_SysMessageResource";

        return SysMessageResourceDao.doQuerySelective(entityName, queryBean,
            joinSQL.toString());
    }

    /**
     * Update Related SysMessageResource
     * @param aSysMessageResource
     * @param aQueryBean
     * @model
     * @throws Exception
     */
    public void updateSysMessageResource(
        com.ourteam.system.dao.SysMessageResource aSysMessageResource,
        SysMessageKeyDAOQueryBean aQueryBean) throws Exception {
        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.system.dao.ISysMessageResourceDAO.MessageKeyId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.system.dao.impl.SysMessageResourceDAO.EntityName +
            " ref_SysMessageResource";

        String[] entityFields = com.ourteam.system.dao.impl.SysMessageResourceDAO.ENTITY_FIELDS;

        String[] entityProperties = com.ourteam.system.dao.impl.SysMessageResourceDAO.ENTITY_PROPERTIES;

        SysMessageResourceDao.doUpdateByQuery(entityName, entityFields,
            entityProperties, joinSQL.toString(), aSysMessageResource,
            aQueryBean);
    }

    /**
     * Delete Related SysMessageResource
     * @param aQueryBean
     * @model
     * @throws Exception
     */
    public void deleteSysMessageResource(SysMessageKeyDAOQueryBean aQueryBean)
        throws Exception {
        StringBuffer joinSQL = new StringBuffer();

        joinSQL.append(" LEFT JOIN ").append(EntityName).append(" ON ( ");

        joinSQL.append(com.ourteam.system.dao.ISysMessageResourceDAO.MessageKeyId)
               .append(" = ").append(Id);

        joinSQL.append(" ) ");

        String entityName = com.ourteam.system.dao.impl.SysMessageResourceDAO.EntityName; // + " ref_SysMessageResource";

        SysMessageResourceDao.doDeleteByQuery(entityName, joinSQL.toString(),
            aQueryBean);
    }

    public static class JdbcSysMessageKeyRowMapper implements RowMapper<SysMessageKey> {
        public SysMessageKey mapRow(ResultSet resultSet, int arg1)
            throws SQLException {
            SysMessageKey object = new SysMessageKey();

            int columnIndex = 1;

            int __dataType;

            try {
                Object id = null;

                id = resultSet.getObject(columnIndex++);

                object.setIdNull(resultSet.getBoolean(columnIndex++));

                if (id != null) {
                    setBeanValue(object, _DataObjectProperty.Id, id);
                }

                Object messageCatalog = null;

                messageCatalog = resultSet.getObject(columnIndex++);

                object.setMessageCatalogNull(resultSet.getBoolean(columnIndex++));

                if (messageCatalog != null) {
                    setBeanValue(object, _DataObjectProperty.MessageCatalog,
                        messageCatalog);
                }

                Object messageKey = null;

                messageKey = resultSet.getObject(columnIndex++);

                object.setMessageKeyNull(resultSet.getBoolean(columnIndex++));

                if (messageKey != null) {
                    setBeanValue(object, _DataObjectProperty.MessageKey,
                        messageKey);
                }

                Object remarks = null;

                remarks = resultSet.getObject(columnIndex++);

                object.setRemarksNull(resultSet.getBoolean(columnIndex++));

                if (remarks != null) {
                    setBeanValue(object, _DataObjectProperty.Remarks, remarks);
                }

                Object status = null;

                status = resultSet.getObject(columnIndex++);

                object.setStatusNull(resultSet.getBoolean(columnIndex++));

                if (status != null) {
                    setBeanValue(object, _DataObjectProperty.Status, status);
                }

                return object;
            } catch (Exception e) {
                throw new SQLException(e.getMessage());
            }
        }
    }
}
